package com.yuexunit.operationman.nbsy.module.Placement.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.DrugBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.adapter.OtherListAdapter;
import com.yuexunit.operationman.nbsy.module.Placement.model.impl.PlacementModel;
import com.yuexunit.operationman.nbsy.module.Placement.presenter.IPlacementPresenter;
import com.yuexunit.operationman.nbsy.module.Placement.presenter.impl.PlacementPresenter;
import com.yuexunit.operationman.nbsy.module.Placement.ui.view.IPlacementView;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/6 0006.
 */

public class Act_Placement_Content extends BaseKJActivity implements IPlacementView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.palcement_listview)
    FixedSwipeListView palcementListview;

    ArrayList<DrugBean> OtherDrugBeans = new ArrayList<>();
    OtherListAdapter otherListAdapter;

    LoadingDialog loadingDialog;
    IPlacementPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_placement_content);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadingDialog = new LoadingDialog(this);
        presenter = new PlacementPresenter(this, new PlacementModel());
        setTitle();
        setContent();

        palcementListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(aty, Act_Placement_Device.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("drug", OtherDrugBeans.get(i));
                bundle.putInt("type", 2);
                bundle.putInt("position", i);
                intent.putExtras(bundle);
                aty.startActivityForResult(intent, 1);
            }
        });
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.PLACEMENTBEAN.getObject().getPatientName());
        if (AppConfig.PLACEMENTBEAN.getObject().getSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.PLACEMENTBEAN.getObject().getSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.PLACEMENTBEAN.getObject().getHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent() {
        if(AppConfig.PLACEMENTBEAN.getObject().getDeviceName()!=null &&
                AppConfig.PLACEMENTBEAN.getObject().getDeviceName().length()>0) {
            String[] device = AppConfig.PLACEMENTBEAN.getObject().getDeviceName().split(";", -1);
            for (int i = 0; i < device.length; i++) {
                DrugBean drugBean = new DrugBean();
                drugBean.setName(device[i]);
                OtherDrugBeans.add(drugBean);
            }

            otherListAdapter = new OtherListAdapter(palcementListview, OtherDrugBeans, R.layout.item_other_list);
            palcementListview.setAdapter(otherListAdapter);
        }
    }


    @OnClick({R.id.titlebar_docadvice_img_back, R.id.bt_add_analgesia, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.bt_add_analgesia:
                Intent intent1 = new Intent(this, Act_Placement_Device.class);
                Bundle bundle1 = new Bundle();
                bundle1.putInt("type", 1);
                intent1.putExtras(bundle1);
                aty.startActivityForResult(intent1, 1);
                break;
            case R.id.sure:
                insertContent();
                break;
        }
    }

    private void insertContent() {
        if (OtherDrugBeans.size() > 0) {
            String device = "";
            for (int j = 0; j < OtherDrugBeans.size(); j++) {
                if (device.length() > 0) {
                    device += ";" + OtherDrugBeans.get(j).getName();
                } else {
                    device += OtherDrugBeans.get(j).getName();
                }
            }
            AppConfig.PLACEMENTBEAN.getObject().setDeviceName(device);
        }
        else if(OtherDrugBeans.size()==0)
        {
            AppConfig.PLACEMENTBEAN.getObject().setDeviceName("");
        }

        if (AppConfig.PLACEMENTBEAN.getObject().getId() == null) {
            AppConfig.PLACEMENTBEAN.getObject().setDeviceType("1");
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.PLACEMENTBEAN));
            presenter.insertPlacementDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.PLACEMENTBEAN));
            presenter.updatePlacementDate(params);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 20000:
                if (data.getExtras().getInt("position") == -1) {
                    DrugBean drug = (DrugBean) data.getExtras().getSerializable("drug");
                    OtherDrugBeans.add(drug);
                    if (otherListAdapter != null) {
                        otherListAdapter.notifyDataSetChanged();
                    } else {
                        otherListAdapter = new OtherListAdapter(palcementListview, OtherDrugBeans, R.layout.item_other_list);
                        palcementListview.setAdapter(otherListAdapter);
                    }
                } else {
                    DrugBean drug = (DrugBean) data.getExtras().getSerializable("drug");
                    OtherDrugBeans.set(data.getExtras().getInt("position"), drug);
                    if (otherListAdapter != null) {
                        otherListAdapter.notifyDataSetChanged();
                    } else {
                        otherListAdapter = new OtherListAdapter(palcementListview, OtherDrugBeans, R.layout.item_other_list);
                        palcementListview.setAdapter(otherListAdapter);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void insertPlacementDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updatePlacementDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
